﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace HYRN.Common.Style
{
    
    public class HelperColor
    {

        /// <summary>
        /// 将字符串转换为Color
        /// </summary>
        /// <param name="color">带#号的16进制颜色</param>
        /// <returns></returns>
        public static Color ToColor(string color)
        {

            int red, green, blue = 0;
            char[] rgb;
            color = color.TrimStart('#');
            color = Regex.Replace(color.ToLower(), "[g-zG-Z]", "");
            switch (color.Length)
            {
                case 3:
                    rgb = color.ToCharArray();
                    red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
                    green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
                    blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
                    return Color.FromArgb(red, green, blue);
                case 6:
                    rgb = color.ToCharArray();
                    red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
                    green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
                    blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
                    return Color.FromArgb(red, green, blue);
                default:
                    return Color.FromName(color);

            }
        }


        /// <summary>  
        /// 三原色光模式(RGB color model)的颜色融合，例如：green + red = yellow,yellow + blue = white  
        /// </summary>  
        /// <param name="c1">颜色1</param>  
        /// <param name="c2">颜色2</param>  
        /// <returns>融合后的颜色color_mixing</returns>  
        private Color ColorMixing(Color c1, Color c2)
        {
            int r = Math.Min(c1.R + c2.R, 255);
            int g = Math.Min(c1.G + c2.G, 255);
            int b = Math.Min(c1.B + c2.B, 255);
            Color color_mixing = Color.FromArgb(Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b));
            return color_mixing;
        }



        public static string GetColorCode(Color color)
        {

            string r = (color.R).ToString("X"); ;
            string g = (color.G).ToString("X"); ;
            string b = (color.B).ToString("X"); ;

            return "#" + r.ToString().PadLeft(2, '0') + g.ToString().PadLeft(2, '0') + b.ToString().PadLeft(2, '0');

        }


    }



}
